package com.pengtu.gsj.service.impl.yh;

import java.text.ParseException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.TecEvalutionDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.yh.TecEvalution;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.yh.TecevalutionService;
import com.pengtu.gsj.utils.DateUtils;

/**
 * 
 * 
 * TecevalutionServiceImpl
 * 
 * 2017年4月18日 下午5:34:03
 * 
 * @version 1.0.0
 * 
 */
@Transactional
@Service
public class TecevalutionServiceImpl implements TecevalutionService {

	@Autowired
	private TecEvalutionDao tecevalutionDao;
	
	@Autowired
	private SystemManageService systemManageService;

	@Override
	public List<TecEvalution> getTecevalutions(QueryFilter queryFilter) {
		return tecevalutionDao.find(queryFilter);
	}

	@Override
	public Page<TecEvalution> findTecevalutions(Page<TecEvalution> page,
			QueryFilter queryFilter) {
		return tecevalutionDao.findPage(page, queryFilter);
	}

	@Override
	public TecEvalution getTecevalution(String id) {
		return tecevalutionDao.get(id);
	}

	@Override
	public void saveTecevalution(TecEvalution tecevalution) {
		tecevalutionDao.save(tecevalution);
	}

	@Override
	public void deleteTecevalution(String id) {
		tecevalutionDao.delete(id);

	}

	@Override
	public void deleteTecevalution(TecEvalution tecEvalution) {
		if (tecEvalution.getAttachments() != null) {
			for (Attachment attachment : tecEvalution.getAttachments()) {
				systemManageService.deleteAttachment(attachment);
			}
		}
		tecevalutionDao.delete(tecEvalution);
	}

	@Override
	public List<TecEvalution> getCheckTrains(String startTime, String endTime)
			throws ParseException {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("tecYear", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("tecYear",  endTime, MatchType.LE);
		}
		return tecevalutionDao.find(queryFilter);
	}

	/**
	 * 
	 * checkTecYear:
	 * 适用:判断年份信息是否已经存在
	 * @param tecYear 用户填写的年份信息
	 * @param id 对应项目id
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public boolean checkTecYear(String tecYear, String id) {
		return tecevalutionDao.checkProperty("tecYear", tecYear, id);
	}

}
